﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PrintDemo.DBUtil;
using PrintDemo.OtherUtil;

namespace PrintDemo.Forms
{
    public partial class DBSettingForm : Form
    {
        public delegate void SaveConfig(bool success);
        public event SaveConfig SaveConfigHandler;
        private DBConnSetting set;
        IniConfig iniConfig = null;
        public DBSettingForm()
        {
            iniConfig = new IniConfig("DbSetting.ini");
            InitializeComponent();
        }

        public DBSettingForm(DBConnSetting set)
        {
            InitializeComponent();
            this.set = set;
            iniConfig = new IniConfig("DbSetting.ini");
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            Valid();
        }
        private bool Valid()
        {
            if (this.txtDBServer.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请输入服务器名称!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else if (txtDB.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请输入数据库名称!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else if (txtUser.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请输入用户名!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else if (txtPsw.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请输入密码!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            DBConnSetting setting = new DBConnSetting(txtDBServer.Text.Trim(), txtDB.Text.Trim(), txtUser.Text.Trim(), txtPsw.Text.Trim());
            if (ValidDbSettingRule.ValidDBSetting(setting))
            {
                MessageBox.Show("测试连接数据库服务器成功！", "连接信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            else
            {
                MessageBox.Show("测试连接数据库服务器失败！", "连接信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Valid())
            {
                if (SaveIni(this.txtDBServer.Text.Trim(), this.txtDB.Text.Trim(), this.txtUser.Text.Trim(), this.txtPsw.Text.Trim()))
                {
                    if (SaveConfigHandler != null)
                        this.SaveConfigHandler(true);
                }
                else
                {
                    if (SaveConfigHandler != null)
                        this.SaveConfigHandler(false);
                }
                this.Close();
            }
        }

        private bool SaveIni(string serverIP, string db, string user, string psw)
        {
            bool success = true;
            try
            {
                iniConfig.Write("Server", serverIP, "MySQLSetting");
                iniConfig.Write("DbName", db, "MySQLSetting");
                iniConfig.Write("User", Base64Util.Base64Encode(Encoding.UTF8, user), "MySQLSetting");
                iniConfig.Write("Psw", Base64Util.Base64Encode(Encoding.UTF8, psw), "MySQLSetting");
                //iniConfig.Write("User", DataProtector.EncryptLocalMachine(user, DataProtector.SALT), "MySQLSetting");
                //iniConfig.Write("Psw", DataProtector.EncryptLocalMachine(psw, DataProtector.SALT), "MySQLSetting");
            }
            catch (Exception ex)
            {
                success = false;
            }
            return success;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (SaveConfigHandler != null)
                this.SaveConfigHandler(false);
            this.Close();
        }
    }
}
